package com.fmdj.snm.controller;

import cn.hutool.core.date.DateTime;
import com.fmdj.common.util.CommonResult;

import com.fmdj.snm.controller.form.ReceiveBillMessageForm;
import com.fmdj.snm.controller.form.SendNewOrderMessageForm;
import com.fmdj.snm.controller.form.SendPrivateMessageForm;
import com.fmdj.snm.entry.MessageEntity;  /*不要导错包 不然传不上值*/
import com.fmdj.snm.task.MessageTask;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/message")
@Tag(name = "MessageController", description = "收发消息模块服务接口")
@Slf4j
public class MessageController {

    @Autowired
    private MessageTask messageTask;

    @PostMapping("/sendPrivateMessage")
    @Operation(summary = "同步发送私有消息")
    public CommonResult sendPrivateMessage(@Valid @RequestBody SendPrivateMessageForm form) {
        MessageEntity entity = new MessageEntity();
        entity.setMsg(form.getMsg());
        entity.setSenderId(form.getSenderId());
        entity.setSenderIdentity(form.getSenderIdentity());
        entity.setSenderName(form.getSenderName());
        entity.setSenderPhoto(form.getSenderPhoto());
        entity.setSendTime(new Date());

        //接收者的交换机以及id
        messageTask.sendPrivateMessage(form.getReceiverIdentity(), form.getReceiverId(), form.getTtl(),entity);

        return CommonResult.ok();
    }

    @PostMapping("/sendPrivateMessageAsync")
    @Operation(summary = "异步发送私有消息")
    public CommonResult sendPrivateMessageAsync(@Valid @RequestBody SendPrivateMessageForm form) {
        MessageEntity entity = new MessageEntity();
        entity.setMsg(form.getMsg());
        entity.setSenderId(form.getSenderId());
        entity.setSenderIdentity(form.getSenderIdentity());
        entity.setSenderName(form.getSenderName());
        entity.setSenderPhoto(form.getSenderPhoto());
        entity.setSendTime(new Date());

        //接收者的交换机以及id
        messageTask.sendPrivateMessageAsync(form.getReceiverIdentity(), form.getReceiverId(), form.getTtl(),entity);

        return CommonResult.ok();
    }

    @PostMapping("/receiveBillMessage")
    @Operation(summary = "同步接收订单账单消息")
    public CommonResult receiveBillMessage(@Valid @RequestBody ReceiveBillMessageForm form) {
        String msg = messageTask.receiveBillMessage(form.getIdentity(), form.getUserId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, msg);
    }
}
